/*
 *  DebugTools.cpp
 *  Rempart
 *
 *
 */


#include "DebugTools.h"

DebugTools* DebugTools::instance(0);

DebugTools * DebugTools::getInstance() {
	
	if (instance == 0) {
		instance = new DebugTools();
	}
	return instance;
}


DebugTools::DebugTools(){
	this->mousePosition = new sf::Vector2<float>();
	this->textDumpCurrentCase = new sf::String();
	this->loop = true;
	this->Launch();

}

DebugTools::~DebugTools(){

	this->loop = false;
	this->Wait();
	delete this->textDumpCurrentCase;
	delete this->mousePosition;
	
}

void DebugTools::generateDumpSprite(){
	char tempTextBuffer[500];
	bool exceptionOccured = false;
	this->mousePosition->x = EventManager::getInstance()->getMousePosition().x;
	this->mousePosition->y = EventManager::getInstance()->getMousePosition().y;
	
	
	int mapCaseX = EventManager::getInstance()->getMouseCasePosition().x;
	int mapCaseY = EventManager::getInstance()->getMouseCasePosition().y;
	
	sprintf(tempTextBuffer,"DEBUG TOOLS:\n\
							Mouse x: %f/%d\n\
							Mouse y: %f/%d\n\
							Case  x: %d/%d\n\
							Case  y: %d/%d\n\
							Map Case x: %d\n\
							Map Case y: %d\n\
							Case layer 1 : %d\n\
							Case layer 2 : %d\n\
							L1 secured : %d\n\
							L2 secured : %d\n\
							Stage : %d\n",
							this->mousePosition->x,   Map::width, 
							this->mousePosition->y,   Map::heigth,
							mapCaseX,Map::width/MapElement::getWidth(),
							mapCaseY,Map::heigth/MapElement::getHeigth(),
							mapCaseX-Menu::width,
							mapCaseY-Menu::heigth,
							Map::getInstance()->getElementType(mapCaseX,mapCaseY),
							Map::getInstance()->getElementType(mapCaseX,mapCaseY,1),
							Map::getInstance()->isSecured(mapCaseX,mapCaseY),
							Map::getInstance()->isSecured(mapCaseX,mapCaseY,1),
							EventManager::getInstance()->getCurrentStage()
			);
	
	this->textDumpCurrentCase->SetText(tempTextBuffer);
	this->textDumpCurrentCase->SetSize(15);
	this->textDumpCurrentCase->SetColor(sf::Color::White);
	this->textDumpCurrentCase->SetPosition(10,Map::heigth-140);
	
}


void DebugTools::Render(sf::RenderTarget& Target) const {
	Target.Draw(*this->textDumpCurrentCase);

}

void DebugTools::Run(){
	while (this->loop) {
		this->generateDumpSprite();
		sf::Sleep(0.01f);
	}
	
}

